package com.youlu.campus.website.controller;

import com.youlu.campus.common.base.QResult;
import com.youlu.campus.website.entity.ContactMessage;
import com.youlu.campus.website.entity.ContactMessagePageQuery;
import com.youlu.campus.website.service.ContactMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 联系我们留言控制层
 *
 * @author su
 * @date 2025/08/30
 */
@RestController
@RequestMapping("/website/contact/message")
public class ContactMessageController {

    @Resource
    private ContactMessageService contactMessageService;

    /**
     * 修改留言
     *
     * @param contactMessage 留言实体
     * @return QResult<ContactMessage>
     */
    @PostMapping("/save")
    public QResult<ContactMessage> saveMessage(@RequestBody ContactMessage contactMessage) {
        if (StringUtils.isBlank(contactMessage.getId())) {
            return QResult.fail("id不能为null");
        }
        ContactMessage save = contactMessageService.saveMessage(contactMessage);
        return QResult.success(save);
    }

    /**
     * 根据 ID 删除留言
     *
     * @param contactMessage 留言 ID
     * @return QResult<Boolean>
     */
    @PostMapping("/delete")
    public QResult<Boolean> deleteMessageById(@RequestBody ContactMessage contactMessage) {
        Boolean b = contactMessageService.deleteMessageById(contactMessage.getId());
        return b ? QResult.success(b) : QResult.fail();
    }

    /**
     * 根据 ID 查询留言详情
     *
     * @param id 留言 ID
     * @return QResult<ContactMessage>
     */
    @GetMapping("/info")
    public QResult<ContactMessage> getMessageById(@RequestParam String id) {
        ContactMessage message = contactMessageService.getMessageById(id);
        return QResult.success(message);
    }

    /**
     * 分页查询留言列表
     *
     * @param pageQuery
     * @return QResult<Page < ContactMessage>>
     */
    @PostMapping("/find-page")
    public QResult<Page<ContactMessage>> findByPage(@RequestBody ContactMessagePageQuery pageQuery) {
        Page<ContactMessage> page = contactMessageService.findByPage(pageQuery);
        return QResult.success(page);
    }

}